<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2020-09-03 09:57:40
 * @LastEditors: ZachGmy
 * @LastEditTime: 2022-06-15 09:49:13
-->
<template>
  <div>
    <!-- 头部按钮 -->
    <div class="herader">
      <el-button
        @click="toAdd"
        type="primary"
        
        size="small"
        >新增</el-button
      >
    </div>
    <!-- 内容展示 -->
    <div class="show">
      <el-table
        :data="dataList"
        border
        style="margin-top: 5px"
        size="small"
        :header-cell-style="{ background: '#185DAD', color: 'white' }"
      >
        <el-table-column type="index" label="序号" align="center" size="small">
        </el-table-column>
        <el-table-column prop="name" label="分类名称" align="center">
        </el-table-column>
        <el-table-column prop="description" label="分类简介" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="toedit(scope.row)" type="primary " size="mini"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="todel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <!-- 表单区域 -->
      <el-form
        :model="listForm"
        :rules="listRlues"
        ref="listForm"
        label-width="100px"
      >
        <!-- 分类标题 -->
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="listForm.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="description">
          <el-input v-model="listForm.description"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="no">
          <el-input v-model="listForm.no"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmint">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      dataList: [],
      // 控制模态框的显示与隐藏
      dialogVisible: false,
      // 设置模态框的标题
      dialogTitle: "",
      // 表单
      listForm: {},
      // 表单验证
      listRlues: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        no: [{ required: true, message: "请输入分类序号", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取新闻分类数据
    async getList() {
      // 设置参数
      let params = {
        page: this.page,
        pageSize: this.pageSize,
      };
      // 发送网络请求
      let res = await get("/category/findAll", params);
      // 数据赋值
      console.log(res);
      this.dataList = res.data;
    },
    // 提交按钮方法
    toSubmint() {
      // 表单验证提交方法
      this.$refs["listForm"].validate(async (valid) => {
        if (valid) {
          // 将表单对象提交到对应的接口中
          let res = await post("/category/saveOrUpdate", this.listForm);
          if (res.status == 200) {
            // 提交成功之后给提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
          } else {
            // 失败给提示
            this.$notify({
              title: "失败",
              message: res.message,
              type: "success",
            });
          }
          // 关闭模态框
          this.dialogVisible=false
          // 刷新页面
          this.getList();
        } else {
          return false;
        }
      });
    },
    // 增加分类方法
    toAdd() {
      // 设置模态框的标题
      this.dialogTitle = "录入新闻分类";
      // 清空表单对象中的数据
      this.listForm = {};
      // 弹出模态框
      this.dialogVisible = true;
    },
    // 编辑分类方法
    toedit(row) {
      // 设置模态框标题
      this.dialogTitle = "编辑新闻分类";
      // 获取数据 赋值到模态框
      this.listForm = { ...row };
      // 弹出模态框
      this.dialogVisible = true;
    },
    // 删除分类方法
    todel(id) {
      // console.log(id);
      // 消息提示的确认
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确定按钮的执行方法
        .then(async () => {
          // 发送网络请求
          let res = await get("/category/deleteById", { id });
          if (res.status == 200) {
            // 请求成功信息
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 刷新页面数据
            this.getList();
          } else {
            // 请求失败提示信息
            this.$message({
              type: "erro",
              message: "删除失败!",
            });
          }
        })
        // 点击取消按钮执行方法
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 新增分类方法
  },
};
</script>

<style scoped>
</style>
